package com.grablist.feihu.grablist.ui.actviity.user;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Map;

import butterknife.Bind;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import com.grablist.feihu.grablist.R;
import com.grablist.feihu.grablist.base.BaseActivity;
import com.grablist.feihu.grablist.bean.ResponInfo;
import com.grablist.feihu.grablist.comm.Constants;
import com.grablist.feihu.grablist.comm.MyApplication;
import com.grablist.feihu.grablist.event.NetErrorEvent;
import com.grablist.feihu.grablist.jswebview.browse.CallBackFunction;
import com.grablist.feihu.grablist.jswebview.browse.JsWeb.CustomWebViewClient;
import com.grablist.feihu.grablist.jswebview.browse.JsWeb.JsHandler;
import com.grablist.feihu.grablist.jswebview.view.ProgressBarWebView;
import com.grablist.feihu.grablist.util.StringUtils;


/**
 * Created by admin on 2018/1/9.
 */

public class WebViewAct extends BaseActivity {


    @Bind(R.id.webview)
    ProgressBarWebView webview;
//    @Bind(R.id.ptr_layout)
//    PtrClassicFrameLayout ptr;
    private ArrayList<String> mHandlers = new ArrayList<String>();
    String url = "";

    @Override
    protected void setRoot() {
        setTitleBar("#FFFFFFFF");
        setContentView(R.layout.ptr_webview);

    }

    @Override
    protected void setup() {
        setStateText();

    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        if (StringUtils.isEmpty(getIntent().getStringExtra(Constants.KEYSTRING))) {
            url = Constants.SHAREURL+"?userCode="+ MyApplication.userInfo.getUserCode();
            setToolBar("邀请好友赢奖励", "");
        } else {
            url = getIntent().getStringExtra(Constants.KEYSTRING)+"?userCode="+ MyApplication.userInfo.getUserCode();
            setToolBar("", "");
        }
        webview.setWebViewClient(new CustomWebViewClient(webview.getWebView()) {

            @Override
            public String onPageError(String url) {
                // 指定网络加载失败时的错误页面
                return url;
            }

            @Override
            public Map<String, String> onPageHeaders(String url) {
                // 可以加入header
                return null;
            }
        });
//        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
//            @Override
//            public void onRefreshBegin(PtrFrameLayout frame) {
//                webview.loadUrl(url);
//                ptr.onRefreshComplete();
//            }
//        });
        // 打开页面，也可以支持网络url
        webview.loadUrl(url);
        mHandlers.add("MyShare");
        // 回调js的方法
        webview.registerHandlers(mHandlers, new JsHandler() {

            @Override
            public void OnHandler(String handlerName, String responseData,
                                  CallBackFunction function) {

                if (handlerName.equals("MyShare")) {
                    ResponInfo response = new Gson().fromJson(
                            responseData, ResponInfo.class);
                    startAct(ShareAct.class,response);
                }

            }

        });


    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    /***
     * Stop location service
     */
    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (webview != null && webview.getWebView() != null) {
            webview.getWebView().destroy();
        }
    }

}

